package com.yzd.learnjava.concurrency;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.yzd.learnjava.util.Print;

public class CloseResource {
	public static void main(String[] args) throws IOException, InterruptedException {
		ExecutorService exec = Executors.newCachedThreadPool(); 
		
		ServerSocket server = new ServerSocket(8084);
		InputStream socketInput = new Socket("localhost",8084).getInputStream();
		
		exec.execute(new IOBlocked(socketInput));
		exec.execute(new IOBlocked(System.in));
		
		TimeUnit.MILLISECONDS.sleep(100);
		Print.print("Shutting down all threads");
		exec.shutdown();
		
		TimeUnit.SECONDS.sleep(1);
		Print.print("Closing " + socketInput.getClass().getSimpleName());
		socketInput.close();
		
		TimeUnit.SECONDS.sleep(1);
		Print.print("Closing " + System.in.getClass().getSimpleName());
		System.in.close();
		
		
	}
}
